package com.coffeeloversupportlibrary.coffeelover.mercury.my.shop;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;

import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.Shop;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.User;
import com.coffeeloversupportlibrary.coffeelover.mercury.my.AddressActivity;
import com.coffeeloversupportlibrary.coffeelover.mercury.my.adapter.ShopAdapter;
import com.coffeeloversupportlibrary.coffeelover.mercury.utils.HttpUtils;
import com.coffeeloversupportlibrary.coffeelover.mercury.utils.JsonUtils;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @author 盛守义
 * @createTime 2021/11/12 14:25
 * @projectName TurningCoffee
 * @className ShopActivity.java
 * @description TODO
 */
public class ShopActivity extends Activity {
    private TextView tv_score;
    private ListView lv_shop;
    private ShopAdapter adapter;
    private Shop shops;

    private int coffeeBeans;
    private User user;
    private SharedPreferences sharedPreferences;

    private Activity activity;

    private HttpUtils httpUtils;
    private JsonUtils jsonUtils;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity_shop);

        initViews();
        getData();


        lv_shop.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });

    }


    void getData(){
        //咖啡豆
        sharedPreferences = getSharedPreferences("user", 0);
        coffeeBeans = sharedPreferences.getInt("coffeeBean", 0);
        Log.e("ShopActivity", "shareCoffeeBean: "+coffeeBeans );
//        tv_score.setText(coffeeBeans+" 个");
        user = (User)getIntent().getSerializableExtra("userInfo");

        Call call = httpUtils.getCall("/user/center-mall/mallList");
        //Call call = httpUtils.getCall("/user/center-mall/mallList");
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                ShopActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(ShopActivity.this, "网络连接崩溃了，绝对绝对不是我们的错", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String res = response.body().string();
                Log.e("ShopActivity", "onResponse: "+res );
                ShopActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        shops = jsonUtils.getShop(res);
                        adapter = new ShopAdapter(shops.getData().getList(), ShopActivity.this, R.layout.my_list_shop, mListenter);
                        lv_shop.setAdapter(adapter);
                    }
                });
            }
        });

    }

    void initViews(){
        shops = new Shop();
        httpUtils = new HttpUtils();
        jsonUtils = new JsonUtils();
        lv_shop = findViewById(R.id.lv_detailedshop);
        activity = ShopActivity.this;
    }

    private ShopAdapter.ShopClickListener mListenter = new ShopAdapter.ShopClickListener() {
        @Override
        public void myOnClick(int position, View v) {
            if(coffeeBeans<=shops.getData().getList().get(position).getMallBeans()) {
                Toast.makeText(activity, "您的咖啡豆余额不足，快去获取咖啡豆吧", Toast.LENGTH_SHORT).show();
            }else{
                Intent intent = new Intent(activity, AddressActivity.class);
                intent.putExtra("shopInfo", shops.getData().getList().get(position));
                startActivity(intent);
            }
        }
    };

}
